<?php

namespace App\Task\Update;

use Hyperf\Crontab\Annotation\Crontab;
use Hyperf\DbConnection\Db;

/**
 * 定时更新消息记录
 * @Crontab(name="UpdateMsgLogTask", rule="22,52 *\/1 * * *", callback="execute", memo="定时更新消息记录", singleton=true)
 */
class UpdateMsgLogTask
{
    public function execute()
    {
        log_info(__CLASS__ . "==Start==" . date('Y-m-d H:i:s'));
        $items = Db::select('SELECT id FROM `t_user_msg_log_tmp` ORDER BY id DESC LIMIT 1');
        $lastId = $items ? $items[0]->id : 0;
        Db::insert("INSERT INTO t_user_msg_log_tmp SELECT * FROM t_user_msg_log WHERE id>{$lastId}");
        $deleteTime = date('Y-m-d H:i:s', time() - 3600 * 24 * 2);
        $deleteRows = Db::delete("DELETE FROM t_user_msg_log_tmp WHERE created_at< '{$deleteTime}'");
        log_info(__CLASS__ . "==LastId:{$lastId}==DeleteRows:{$deleteRows}");
        return;
    }
}
